// 负责用户业务逻辑处理
const userModel = require('../models/userModel');
// 导入uuid功能（生成随机字符串）
const { v4: uuidv4 } = require('uuid');
const Jwt = require('../common/jwt');
// 定义用户类
class User {
    // 1. 注册
    async register(req, res) {
        let user_acc = req.body.user_acc;
        let user_pwd = req.body.user_pwd;
        let user_nick_name = req.body.user_nick_name;
        // 处理用户信息
        let user_id = uuidv4();
        let dt = new Date();
        let user_create_time = dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
        if (user_acc == undefined || user_pwd == undefined || user_nick_name == undefined) {
            res.send({
                code: -1,
                msg: '注册时缺少参数'
            })
        }
        else {
            await userModel.register(user_id, user_acc, user_pwd, user_nick_name, user_create_time)
                .then(
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    err => {
                        res.send(err)
                    }
                )
        }
    }
    // 2. 登录
    async login(req, res) {
        let user_acc = req.body.user_acc;
        let user_pwd = req.body.user_pwd;
        if (user_acc == undefined || user_pwd == undefined) {
            res.send({
                code: -1,
                msg: '请填写账号或密码'
            })
        }
        else {
            userModel.login(user_acc, user_pwd)
                .then(
                    // 账号和密码在此处做判断
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    // 执行sql语句出现异常
                    err => {
                        res.send(err);
                    }
                )
        }
    }
    // 3. 注销
    async remove(req, res, next) {
        //获取用户临时身份
        let token = req.headers.token || req.body.token;
        //  let token = req.body.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        console.log(result);
        if (result.data) {
            // 记录用户user_id
            let user_id = result.data;
            // 登录状态
            await userModel.remove(user_id)
                .then(
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    err => {
                        res.send(err);
                    }
                )
        }
        else {
            // 未登录状态
            res.send({
                code: -1,
                msg: '请先登录账号。'
            })
        }

        next();
    }
    // 4. 获取用户头像
    async getPicUrl(req, res) {
        // let token = req.headers.token;
        let token = req.query.token || req.headers.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        // console.log("2.0",result);
        // 判断用户id是否值
        if (result.data) {
            let user_id = result.data;
            // 获取真实用户头像
            await userModel.getPicUrl(user_id)
                .then(
                    content => {
                        res.send(content)
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '登录已过期,请重新登录'
            })
        }

    }

    // 获取头像（通过用户user_id字段）
    async getImage(req, res) {
        let user_id = req.query.user_id;
        // 获取真实用户头像
        await userModel.getPicUrl(user_id)
            .then(
                content => {
                    res.send(content)
                }
            )
    }

    // 5. 获取用户资料
    async getUserInfo(req, res) {
        // let token = req.headers.token;
        let token = req.query.token || req.headers.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        // 判断用户id是否值
        if (result.data) {
            let user_id = result.data;
            // 获取真实用户头像
            await userModel.getUserInfo(user_id)
                .then(
                    content => {
                        res.send(content)
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '登录已过期,请重新登录'
            })
        }
    }
    // 6. 设置用户头像
    async setPicUrl(req, res) {
        let token = req.body.token || req.headers.token;
        let user_picUrl = req.body.user_picUrl ;
        // let token = req.headers.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        console.log(result);
        if (result.data) {
            // 获取用户身份
            let user_id = result.data;
            // 设置用户头像
            userModel.setPicUrl(user_id, user_picUrl).then(
                data => {
                    res.send(data);
                }
            )
                .catch(
                    err => {
                        res.send(err);
                    }
                )
        }
        else {
            res.send({
                code: -1,
                msg: '登录已过期,请重新登录'
            })
        }
    }
    // 7. 设置用户资料
    async setUserInfo(req, res) {
        // let token = req.headers.token;
        //  解构赋值
        let { user_nick_name, user_sex, user_age, user_address } = req.body;
        let token = req.body.token || req.headers.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        // 判断用户id是否值
        if (result.data) {
            let user_id = result.data;
            if (user_nick_name == undefined || user_sex == undefined || user_age == undefined || user_address == undefined) {
                res.send({
                    code: -1,
                    msg: '修改用户资料缺少参数'
                })
            }
            else {
                // 获取真实用户头像
                await userModel.setUserInfo(user_id, user_nick_name, user_sex, user_age, user_address)
                    .then(
                        content => {
                            res.send(content)
                        }
                    )
                    .catch(
                        err => {
                            res.send(err);
                        }
                    )
            }
        }
        else {
            res.send({
                code: -1,
                msg: '登录已过期,请重新登录'
            })
        }
    }
    // 8. 找回密码
    async findPwd(req, res) {
        let user_acc = req.body.user_acc;
        let new_user_pwd = req.body.new_user_pwd;//新密码
        await userModel.findPwd(user_acc, new_user_pwd)
            .then(
                content => {
                    res.send(content)
                }
            )
            .catch(
                err => {
                    res.send(err);
                }
            )
    }
    // 9. 邮箱登录
    async emailLogin(req, res) {
        let user_acc = req.body.user_acc || req.body.email;
        if (user_acc == undefined) {
            res.send({
                code: -1,
                msg: '请填写账号或密码'
            })
        }
        else {
            userModel.emailLogin(user_acc)
                .then(
                    // 账号和密码在此处做判断
                    content => {
                        res.send(content)
                    }
                )
                .catch(
                    // 执行sql语句出现异常
                    err => {
                        res.send(err);
                    }
                )
        }
    }
    // 4. 判断用户是否在线
    async onLine(req, res) {
        // let token = req.headers.token;
        let token = req.query.token || req.headers.token;
        let jwt = new Jwt(token);
        // 判断token是否在有效期
        let result = jwt.verifyToken();
        console.log("2.0",result);
        // 判断用户id是否值
        if (result.data) {
            let user_id = result.data;
            userModel.onLine(user_id).then(
                content => {
                    res.send(content)
                },
                err => {
                    res.send({
                        code: 500,
                        msg: '服务端异常。'
                    })
                }
            )

        }
        else {
            res.send({
                code: -1,
                msg: '登录已过期。'
            })
        }
    }
}
// 暴露用户类的实例
// 把用户对象挂载到系统对象
module.exports = new User();